package com.example.ffmpegplayer;

import android.os.Bundle;
import android.util.Log;
import android.widget.SeekBar;

import androidx.appcompat.app.AppCompatActivity;

import com.example.ffmpegplayer.databinding.ActivityMainBinding;
import com.example.ffmpegplayer.player.IPlayer;
import com.example.ffmpegplayer.player.IPlayerCallback;
import com.example.ffmpegplayer.player.PlayerImpl;
import com.example.ffmpegplayer.utils.MediaUtil;

public class MainActivity extends AppCompatActivity {


    private static final String TAG = MainActivity.class.getSimpleName();
    private ActivityMainBinding binding;
    private boolean isSeek;
    private int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        IPlayer player = new PlayerImpl();
        player.setSurfaceView(binding.mediaSurface);
        //       player.setDataSource("http://devimages.apple.com.edgekey.net/streaming/examples/bipbop_4x3/gear2/prog_index.m3u8");
        //     player.setDataSource("http://live.ximalaya.com/radio-first-page-app/live/265/64.m3u8");
//        File file = new File(Environment.getExternalStorageDirectory(),"test.mp3");
//        Log.d("url = ",file.getAbsolutePath());
        // http://devimages.apple.com.edgekey.net/streaming/examples/bipbop_4x3/gear2/prog_index.m3u8
        player.setDataSource("/sdcard/input.mp4");
        player.registerIPlayerCallback(new IPlayerCallback() {
            @Override
            public void onParpared() {

                Log.d(TAG, "onParpared");
                player.start();
            }

            @Override
            public void onLoad(boolean load) {
                Log.d("MainActivity", String.valueOf(load));
            }

            @Override
            public void onPlaying(boolean isPlaying) {
                Log.d(TAG, "isPlaying");
                String text = isPlaying ? "暂停" : "播放";
                binding.playOrPause.setText(text);
                if (isPlaying) {
                    binding.playStart.setText("停止");
                }
            }

            @Override
            public void onCurrentTime(int currentTime, int totalTime) {
                if (!isSeek && totalTime > 0) {
                    binding.seekbar.setProgress(currentTime * 100 / totalTime);
                    binding.startTime.setText(MediaUtil.secdsToDateFormat(currentTime));
                    binding.endTime.setText(MediaUtil.secdsToDateFormat(totalTime));

                }
            }

            @Override
            public void onError(int code, String msg) {

            }

            @Override
            public void onFinish() {

            }
        });
        binding.playStart.setOnClickListener(v -> {
            String text = binding.playStart.getText().toString().trim();
            if (text.equals("开始")) {
                player.prepareAsync();
            } else {
                player.stop();
                binding.playStart.setText("停止");
            }
        });
        binding.playOrPause.setOnClickListener(v -> {
            String text = binding.playOrPause.getText().toString().trim();
            if (text.equals("暂停")) {
                player.pause();
            } else {
                player.resume();
            }
        });
        binding.playSwitch.setOnClickListener(v -> {
            String text = binding.playSwitch.getText().toString().trim();
            if (text.equals("立体音")) {
                player.setChannelMute(1);
                binding.playSwitch.setText("右声道");
            } else if (text.equals("右声道")) {
                player.setChannelMute(2);
                binding.playSwitch.setText("左声道");
            } else {
                player.setChannelMute(0);
                binding.playSwitch.setText("立体音");
            }
        });
        binding.volumeMute.setOnClickListener(v -> {
            String text = binding.volumeMute.getText().toString().trim();
            if (text.equals("静音")) {
                player.setVolumeMute(true);
                binding.volumeMute.setText("解除静音");
            } else {
                player.setVolumeMute(false);
            }
        });
        binding.volumeAdd.setOnClickListener(view -> {
            int currentVolume = player.getCurrentVolume();
            Log.d(TAG, "当前音量  " + currentVolume);
            if (currentVolume >= 100) {
                Log.d(TAG, "当前是最大音量  ");
                return;
            }
            player.setVolume(currentVolume + 10);
        });
        binding.volumeMinus.setOnClickListener(view -> {
            int currentVolume = player.getCurrentVolume();
            Log.d(TAG, "当前音量  " + currentVolume);
            if (currentVolume <= 0) {
                Log.d(TAG, "当前是最小音量  ");
                return;
            }
            player.setVolume(currentVolume - 10);
        });
        binding.seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                position = progress * player.getDuration() / 100;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                isSeek = true;
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Log.d(TAG, "position  " + position);
                player.seekTo(position);
                isSeek = false;
            }
        });
        binding.playSpeed.setOnClickListener(view -> {
            String speed = binding.playSpeed.getText().toString().trim();
            switch (speed) {
                case "1.0":
                    player.setSpeed(1.2f);
                    binding.playSpeed.setText("1.2");
                    break;
                case "1.2":
                    player.setSpeed(1.5f);
                    binding.playSpeed.setText("1.5");
                    break;
                case "1.5":
                    player.setSpeed(1.8f);
                    binding.playSpeed.setText("1.8");
                    break;
                case "1.8":
                    player.setSpeed(2.0f);
                    binding.playSpeed.setText("2.0");
                    break;
                case "2.0":
                    player.setSpeed(1.0f);
                    binding.playSpeed.setText("1.0");
                    break;
            }
        });
    }
}